﻿using AutoMapper;
using GPS.DataAccessLayer;
using GPS.ENTITYS;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using PagedList;
using GPS.COMMON;
using System.IO;
using System.Web.UI;
using GPS.GPS_DAL;

namespace GPS.Controllers.GPS_Export
{
    public class GPS_BC_DSDuSatHachController : Controller
    {
        //
        // GET: /GPS_BC_DSDuSatHach/
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ActionResult Index(View_Export_DSDuSatHach SearchModel, int? currentPage)
        {
            //sử dụng:
            initialCategorySearchAction(SearchModel);
            //var entities = _db.Filter<Export_HopDongDaoTaoLX>(o => (SearchModel.dDVGTVT_MA == null || o.DVGTVT_MA.StartsWith(SearchModel.dDVGTVT_MA))
            //                            && (SearchModel.hHV_MA == null ||  o.DTSH_TTSH_HV_MA.StartsWith(SearchModel.hHV_MA)));
            //ViewBag.TotalRow = entities.Count();
            //var model = new List<Export_HopDongDaoTaoLX>();
            //foreach (var currentEntity in entities)
            //{
            //    model.Add(Mapper.Map<Export_HopDongDaoTaoLX>(currentEntity));
            //}
            // var pageIndex = SearchModel.Page ?? 1;
            //  SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            return View(SearchModel);
        }
        public void initialCategorySearchAction(View_Export_DSDuSatHach SearchModel)
        {
            ViewBag.lstDVGTVT = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        }

        public DataSet GetData_DSDuSatHach(string connectionString, string param1)
        {
            DataSet ds = OracleHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, "GPS_EXPORT.GPS_EXPORT_DSDUSATHACH"
                                                     , new OracleParameter("pDVGTVT_MA", OracleDbType.Varchar2, string.IsNullOrWhiteSpace(param1) ? DBNull.Value : (object)param1, ParameterDirection.Input)
                                                     , new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output)
                                                    );
            return ds;
        }

        public ActionResult ExportToWord(View_Export_DSDuSatHach SearchModel, FormCollection form)
        {
            //string strDDLValue = form["DVGTVT_MA_Dll"].ToString();
            SearchModel.dDVGTVT_MA = "40023";
            //DataSet ds = (new GPS_BaoCaoDAO()).GetData_HopDongDaoTao(_db.DbContext.Database.Connection.ConnectionString, SearchModel.dDVGTVT_MA, SearchModel.hHV_MA);
            DataSet ds = GetData_DSDuSatHach(_db.DbContext.Database.Connection.ConnectionString, SearchModel.dDVGTVT_MA);
            Export_DSDuSatHach _export = new Export_DSDuSatHach();

            _export.DVGTVT_MA = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MA == SearchModel.dDVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            _export.DATETIME_NOW = DateTime.Now.ToString();
            _export.LIST_DSDUSATHACH = COMMON.Helpers.MapDataTableToList<DSDuSatHach>(ds.Tables[0]);

            //string dd = "";
            //foreach (var item in _export.LIST_THIMOI)
            //{
            //    dd = item.KH_SOQDKG;
            //}
            //_export.KH_SOQDKG = dd;
            //_export.LIST_CHUATHI = COMMON.Helpers.MapDataTableToList<Export_BC_TC_ChuaThi>(ds.Tables[1]);
            //_export.LIST_VANGTHI = COMMON.Helpers.MapDataTableToList<Export_BC_TC_VangThi>(ds.Tables[2]);
            //_export.LIST_THILAI = COMMON.Helpers.MapDataTableToList<Export_BC_TC_ThiLai>(ds.Tables[3]);

            byte[] contentFile = null;
            //string contentType = "application/msword";
            contentFile = GPS.ENTITYS.EXPORTWORD_DSDuSatHach.EXPORT_DSDuSatHach(Server.MapPath("~/Templates/GPS_Word/DanhSachDuSatHachCapGPLX.docx"), _export, "DocxGenericReport");
            string nameOutput = "DanhSachDuSatHachCapGPLX_" + DateTime.Now.ToShortDateString() + ".docx";
            return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);
        }
	}
}